//
//  TableViewController.m
//  detail
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 NB三人行. All rights reserved.
//

#import "DetailTableViewController.h"
#import "DetailCell.h"
#import "Detail.h"
#import "UIImageView+WebCache.h"
#import "FoodDetail.h"
#import "ImageDownLoader.h"
#import "UIImageView+WebCache.h"
#import "MainCollectionViewController.h"
@interface DetailTableViewController ()
@property (nonatomic, strong)UIView *stepCellHeaderView;

@property (nonatomic, strong) UILabel *materialLabel;

@property (nonatomic,strong) Detail *detail;

@property (nonatomic,strong) NSMutableArray *urlImageArray;




@end

@implementation DetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Allclass_background.jpg"]];
    
    [self headView];
    [self imageURL];

}

//取出图片网址

-(void)imageURL{
    
    self.urlImageArray = [NSMutableArray array];
    self.urlArray = [NSMutableArray array];
    for (NSDictionary *dic in self.stepsArray) {
        
        FoodDetail *detail = [[FoodDetail alloc] init];
        detail.image_url = [dic objectForKey:@"img"];
        [self.urlImageArray addObject:detail.image_url];
        [self.urlArray addObject:detail];
    }
    
    [self.tableView reloadData];

}

//创建headerView 
-(void) headView{
    
//材料
  
    self.materialLabel = [[UILabel alloc] init];
    self.materialLabel.numberOfLines = 0;
    self.materialLabel.font = [UIFont systemFontOfSize:22];
    self.materialLabel.textAlignment = NSTextAlignmentLeft;
    self.materialLabel.text = self.burden;
    self.materialLabel.font = [UIFont systemFontOfSize:18];
    CGSize size = [self sizeWithString:self.materialLabel.text font:self.materialLabel.font];
    self.materialLabel.frame = CGRectMake(0, 315, size.width , size.height);
    self.stepCellHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, size.height+410)];
//图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 285)];
//    imageView.backgroundColor = [UIColor redColor];
    imageView.image = self.image;
    [self.stepCellHeaderView addSubview:imageView];
//菜名
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 255, 375, 30)];
    [self.stepCellHeaderView addSubview:nameLabel];
    nameLabel.text = self.title;
    nameLabel.textColor = [UIColor redColor];
    nameLabel.font = [UIFont systemFontOfSize:22];
    [imageView addSubview:nameLabel];
//材料标题
    UILabel *materialTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 285, 375, 30)];
    materialTitleLabel.text = @"调味料";
    materialTitleLabel.font = [UIFont systemFontOfSize:20];
    [self.stepCellHeaderView addSubview:materialTitleLabel];
    //将材料添加上
    [self.stepCellHeaderView addSubview:self.materialLabel];
    
//食材
    
    UILabel *ingredientsLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 340, 375, 30)];
    ingredientsLable.text = @"食材";
    ingredientsLable.font = [UIFont systemFontOfSize:20];
    
    // 食材详情
    
    UILabel *ingredientsTextLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 370, size.width, size.height)];
    ingredientsTextLable.numberOfLines = 0;
    //    self.materialLabel.font = [UIFont systemFontOfSize:22];
    ingredientsTextLable.textAlignment = NSTextAlignmentLeft;
    ingredientsTextLable.text = self.ingredients;
    ingredientsTextLable.font = [UIFont systemFontOfSize:18];
    
    [self.stepCellHeaderView addSubview:ingredientsLable];
    //将材料添加上
    [self.stepCellHeaderView addSubview:ingredientsTextLable];

    
   
//步骤cell的标题
    UILabel *stepTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 375+size.height, 375, 30)];
    stepTitleLabel.text = @"步骤";
    stepTitleLabel.font = [UIFont systemFontOfSize:20];
    [self.stepCellHeaderView addSubview:stepTitleLabel];
//将headerView添加到步骤cell上
    self.tableView.tableHeaderView = self.stepCellHeaderView;
    
}




//字符串自适应高度，自动换行
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font
{
CGRect rect = [string boundingRectWithSize:CGSizeMake(375, 8000)//限制最大的宽度和高度
                                   options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
                                       context:nil];
    return rect.size;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    return self.stepsArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"reuseIdentifier";
    
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.stepDetailLabel.text = [self.stepsArray[indexPath.row] objectForKey:@"step"];
    
    cell.stepDetailLabel.numberOfLines = 0;
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Allclass_background.jpg"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.stepImage sd_setImageWithURL:self.urlImageArray[indexPath.row]];

    return cell;
}






/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
